package com.test.lyt.controller;

import com.amazonaws.ClientConfiguration;
import com.amazonaws.Protocol;
import com.amazonaws.auth.BasicAWSCredentials;
import com.amazonaws.services.s3.AmazonS3;
import com.amazonaws.services.s3.AmazonS3Client;
import com.amazonaws.services.s3.S3ClientOptions;
import com.amazonaws.services.s3.model.GeneratePresignedUrlRequest;
import com.amazonaws.services.s3.model.ListObjectsV2Request;
import com.amazonaws.services.s3.model.ListObjectsV2Result;
import com.amazonaws.services.s3.model.S3ObjectSummary;
import org.apache.http.client.utils.DateUtils;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;

public class S3example {
    private final static String END_POINT = "https://obs-helf.cucloud.cn";//请替换成您Bucket所在区域的endpoint

    public static void main(String[] args) {

        BasicAWSCredentials basicAwsCred = new BasicAWSCredentials("6CCB631CFE81471389EA7C7D0EE62A3B3708", "1FE8B67CB73C45B697FCA47EA42DD20D7306"); //请替换成您的AccessKey和SecretKey
        ClientConfiguration clientConfiguration = new ClientConfiguration();
        clientConfiguration.setProtocol(Protocol.HTTPS);
        AmazonS3Client s3 = new AmazonS3Client(basicAwsCred, clientConfiguration);
        S3ClientOptions options = S3ClientOptions.builder().setPathStyleAccess(true).setPayloadSigningEnabled(true).disableChunkedEncoding().build();
        s3.setS3ClientOptions(options);
        s3.setEndpoint(END_POINT);

        S3example s3example = new S3example();
        s3example.listObjects(s3);
    }

    public void listObjects(AmazonS3 s3) {
        // 初始化 ListObjectsV2Request
        ListObjectsV2Request listObjectsV2Request = new ListObjectsV2Request()
                .withBucketName("test-common-private")
                .withPrefix("rule"); // 可选：指定前缀以过滤对象


        // 用于存储所有对象的摘要信息
        List<S3ObjectSummary> objectSummaries = new ArrayList<>();

        // 用于分页的延续令牌
        String continuationToken = null;

        do {
            // 设置请求中的延续令牌
            if (continuationToken != null) {
                listObjectsV2Request.setContinuationToken(continuationToken);
            }

            // 获取当前页的对象列表
            ListObjectsV2Result result = s3.listObjectsV2(listObjectsV2Request);

            // 收集当前页的对象摘要信息
            objectSummaries.addAll(result.getObjectSummaries());

            // 更新 continuationToken
            continuationToken = result.getNextContinuationToken();
        } while (continuationToken != null);

        // 输出结果
        for (S3ObjectSummary summary : objectSummaries) {
            System.out.println("Key: " + summary.getKey() + ", Size: " + summary.getSize());
        }

    }

    public void getObjectUrl(AmazonS3 s3) {
        String myKeyName = "1.txt";
        GeneratePresignedUrlRequest request = new GeneratePresignedUrlRequest("test-common-private", "rule/1.txt");
        //设置链接过期时间
        request.setExpiration(DateUtils.parseDate(LocalDateTime.now().plusDays(7).toString()));
        //生成共享链接
        String s = s3.generatePresignedUrl(request).toString();
        System.out.println("连接是"+s);
    }


    public void downObject(AmazonS3 s3) {
//        s3.download()
    }
}